package com.eascs.crm.nsEditDetail.service;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.NsEditDetailDao;
import com.eascs.crm.nsEditDetail.entity.AssetsTdInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CarInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CarInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.AssetsTdInfoForm;
import com.eascs.crm.nsEditDetail.form.CarInfoForm;
import com.eascs.web.sso.UserHolder;

@Service
public class AssetsTdInfoService {

	@Inject
	NsEditDetailDao nsEditDetailDao;

	public PageResult<Object[]> findAssetsTdListInfo(String loanapplyId,Integer showPage) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, showPage);
		StringBuilder sql = new StringBuilder();
		sql.append("select t.id, t.types,t.loan_bc,t.tot_price,t.over_price,t.dp_type,concat(t.start_date,'~',t.end_date),t.fk_type from assets_td_info t  where 1=1 and t.is_del = 'N' ");
		if (StringUtil.isNotBlank(loanapplyId)) {
			sql.append(" and t.loanapply_no = ?");
			sqlPageQuery.addParam(loanapplyId);
		}
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> CarPage = nsEditDetailDao.findDataWithPage(sqlPageQuery);
		return CarPage;
	}
	
	public AssetsTdInfoEntity findAssetsTdInfoEntity(String assetsTdId) {
		return nsEditDetailDao.getById(AssetsTdInfoEntity.class, assetsTdId);
	}
	
	public AssetsTdInfoEntity findAssetsTdInfoByLoanapplyId(String loanapplyId) {
		return nsEditDetailDao.findEntity("select * from assets_td_info where loanapply_no = ?",
				AssetsTdInfoEntity.class, loanapplyId);
	}

	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public boolean assetsTdDel(String assetsTdId){
		int i = nsEditDetailDao.execute("update assets_td_info set is_del ='Y' where id = ? ", assetsTdId);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public boolean assetsTdSave(AssetsTdInfoEntity assetsTd,AssetsTdInfoForm form) {
		setAssetsTdPorperty(assetsTd, form);
		int i = nsEditDetailDao.insert(assetsTd);
		if(i>0){
			return  true;
		}else{
			return  false;
		}
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public boolean assetsTdUpdate(AssetsTdInfoEntity assetsTd,AssetsTdInfoForm form) {
		setAssetsTdPorperty(assetsTd, form);
		int i = nsEditDetailDao.updateById(assetsTd);
		if(i>0){
			return  true;
		}else{
			return  false;
		}
	}


	private void setAssetsTdPorperty(AssetsTdInfoEntity assetsTd, AssetsTdInfoForm form) {
		assetsTd.setTypes(form.getTypes());
		assetsTd.setLoanBc(form.getLoanBc());
		assetsTd.setTotPrice(form.getTotPrice());
		assetsTd.setOverPrice(form.getOverPrice());
		assetsTd.setDpType(form.getDpType());
		assetsTd.setStartDate(form.getStartDate());
		assetsTd.setEndDate(form.getEndDate());
		assetsTd.setFkType(form.getFkType());
		assetsTd.setIsDel("N");
		assetsTd.setLoanapplyNo(StringUtil.isBlank(form.getLoanapplyId())?assetsTd.getLoanapplyNo():form.getLoanapplyId());
		assetsTd.setId(form.getAssetsTdId());
	}




}
